<?php
/************************************/
// Category controller
// thaodx@gmail.com
/************************************/

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Category extends CI_Controller {
	public function index(){
		$this->load->model('Category_model', 'cateModel');
		if(isset($_POST['cmd'])){
			if($_POST['cmd'] == 'reorder' && is_array($_POST['category_order'])){
				if($this->cateModel->reorderCategory($_POST['category_order'])){
					$data['msg'] = 'Update category order successful.';
				}
			}
			elseif($_POST['cmd'] == 'delete' && is_array($_POST['item'])){
				if($this->cateModel->deleteCategory($_POST['item'])){
					$data['msg'] = 'Delete category successful.';
				}
			}
		}
		$cate = $this->cateModel->getAllCategory();
		$this->layout->title('Category / List / ');
		$data['cateRow'] = $this->cateModel->buildListCatRecursive(array_reverse($cate));

		$this->layout->view('category_index', $data);// Render view and layout
	}
	public function add(){
		$this->load->model('Category_model', 'cateModel');
		if(isset($_POST['category_name'])){
			$this->cateModel->addCategory($_POST);
			$data['msg'] = 'Add new category successful.';
		}
		$cate = $this->cateModel->getAllCategory();
		$data['cateOpt'] = $this->cateModel->buildOptCatRecursive(array_reverse($cate));
		$this->layout->title('Category / Add new / ');
		$this->layout->view('category_add', $data);// Render view and layout
	}
	public function edit($cid){
		$this->load->model('Category_model', 'cateModel');
		if(isset($_POST['category_name'])){
			$this->cateModel->editCategory($_POST);
			$data['msg'] = 'Edit category successful.';
		}
		// get cate detail
		$data['cateInfo'] = $this->cateModel->getDetailCategory($cid);

		$cate = $this->cateModel->getAllCategory();
		$data['cateOpt'] = $this->cateModel->buildOptCatRecursive(array_reverse($cate), 0, intval($data['cateInfo']['category_parent']));
		$this->layout->title('Category / Edit / ');
		$this->layout->view('category_edit', $data);// Render view and layout
	}
}